import os,sys,time
from rasplab.common import tcpsrv
from rasplab.common import gpio


class PWMLed():
	def __init__(self):
		self.gpio = gpio.RaspGPIO(pins=[3,5],frequence=100)

	def run(self):
		self.gpio.start()
		server = tcpsrv.TcpServer()
		server.listen(self)

	def msg_execute(self,message):
		#print 'execute message: '+message
		rawArray = message[0:len(message)-1].split(':')
		pin = (3 if rawArray[0] == 'thr' else 5)
		dc = rawArray[1].strip()

		if dc.isdigit():
			print 'changing pin{0} dc into {1}'.format(pin,int(dc))
			self.gpio.change(pin, int(dc))

	def msg_bye(self):
		self.gpio.stop()
		print 'gpio stopped.'

	def msg_invalid(self,message):
		print 'invalid message detected in machine module'
		print message


if __name__ == '__main__':
	led = PWMLed()
	led.run()
		




